import { defineComponent } from 'vue';
import SampleComponentBase from '/@/shared/component-base/sample-component-base';
import { IListViewColumnsDto } from '/@/shared/service-proxies';
import { jsonToSheetXlsx } from '/@/components/Excel';
export default defineComponent({
  mixins: [SampleComponentBase],
  data() {
    return {
      /** 导出文件名 */
      //   excelTitle: 'Data',
    };
  },
  emits: [],
  computed: {
    formartColumns() {
      return (listColumns: any) => {
        const columns: Array<IListViewColumnsDto> = listColumns
          .filter((item: IListViewColumnsDto) => {
            return item.type != 'action' && !!item.ifShow;
          })
          .sort((a, b) => {
            return a.sortNum - b.sortNum;
          });
        return columns;
      };
    },
    formatExcelHeader() {
      return (listColumns: any) => {
        const obj = {};
        this.formartColumns(listColumns).map((i: IListViewColumnsDto) => {
          obj[i?.field ?? i?.type] = this.l(i?.title ?? '');
        });
        return obj;
      };
    },
    formatExcelData() {
      return (type: string, listColumns: any, data: any) => {
        const columns = this.formartColumns(listColumns);
        return data.map((record, index) => {
          const obj = {};
          columns.forEach((column) => {
            if (column.type == 'no') {
              // 序号
              if (this.getShowPagination() && type == 'Export') {
                obj[column.type] =
                  (this.pagination.current - 1) * this.pagination.pageSize + (index + 1);
              } else {
                obj[column.type] = index + 1;
              }
            } else if (column.type == 'datetime') {
              // 时间
              obj[column.field] = this._moment(
                this.exportConfig.formartColumn(column.field, record[column.field]),
                column.dateFormat,
              );
            } else {
              // 其他
              obj[column.field] = this.exportConfig.formartColumn(
                column.field,
                record[column.field],
              );
            }
          });
          return obj;
        });
      };
    },
  },
  methods: {
    /** 导出当前页 */
    export(columns: any, data: any) {
      console.log(columns, data);

      jsonToSheetXlsx({
        header: columns,
        data,
        filename: `${this.exportConfig?.filename ?? 'Data'}.xlsx`,
      });
      abp.notify.success(this.l('ExportSuccess'));
    },
    /** 导出全部 */
    exportAll() {},
  },
});
